package com.huaiwei.usmManager.modules.usm.controller;

import com.huaiwei.usmManager.common.utils.R;
import com.huaiwei.usmManager.modules.usm.entity.GoodsEntity;
import com.huaiwei.usmManager.modules.usm.entity.GoodsSpecEntity;
import com.huaiwei.usmManager.modules.usm.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理 ")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    @ApiOperation("分页查询商品列表")
    public R list(@RequestParam Map<String,Object> params){
        return R.ok().put("page",goodsService.getPage(params));
    }

    @GetMapping("/info/{goodsId}")
    @ApiOperation("根据商品id查询所有信息")
    public R info(@PathVariable("goodsId") Integer goodsId){
        return R.ok().put("data",goodsService.getInfoByGoodsId(goodsId));
    }

    @PostMapping("/update")
    @ApiOperation("添加或者修改商品")
    public R insertOrUpdate(@RequestBody GoodsEntity goodsEntity){
        goodsService.insertOrUpdate(goodsEntity);
      return R.ok();
    }

    @DeleteMapping("/delete/{goodsId}")
    @ApiOperation("根据id删除商品信息")
    public R delete(@PathVariable("goodsId") Integer goodsId){
        goodsService.deleteInfo(goodsId);
        return R.ok();
    }

    @PostMapping("/status")
    @ApiOperation("改变商品的状态")
    public R status(@RequestBody GoodsEntity goodsEntity){
        goodsService.updateById(goodsEntity);
        return R.ok();
    }

    @GetMapping("/info/spec/{prospecId}")
    @ApiOperation("根据规格id查出规格名字")
    public R specInfo(@PathVariable("prospecId") Integer prospecId){
        return R.ok().put("goodsPack",goodsService.getProSpecNameByProspecId(prospecId));
    }

    @GetMapping("/message/{cabinetId}")
    @ApiOperation("根据展示柜的id查出具体的店铺和柜子信息")
    public R message(@PathVariable("cabinetId") Integer cabinetId){
        return R.ok().put("message",goodsService.getMessageByCabinetId(cabinetId));
    }

    @GetMapping("/sales/list/{goodsId}")
    @ApiOperation("根据goodsId查出他的销售记录")
    public R salesList(@PathVariable("goodsId") Integer goodsId,@RequestParam Map<String,Object> params){
        return R.ok().put("page",goodsService.getSalesByGoodsId(goodsId,params));
    }

    @GetMapping("/cascade/allshop")
    @ApiOperation("级联查询先查所有的shop")
    public R cascade1(){
        return R.ok().put("list",goodsService.getAllShop());
    }

    @GetMapping("/cascade/mycabinet/{shopId}")
    @ApiOperation("级联查询查出所属的cabinet")
    public R cascade2(@PathVariable("shopId") Integer shopId){
        return R.ok().put("list",goodsService.getAllCabinetByShopId(shopId));
    }

    @GetMapping("/spec/list")
    @ApiOperation("下拉框查询出所有的规格")
    public R specList(){
        return R.ok().put("list",goodsService.getAllSpec());
    }

    @GetMapping("/type/list")
    @ApiOperation("下拉框查询出所有的分类")
    public R typeList(){
        return R.ok().put("list",goodsService.getAllType());
    }

    @PostMapping("/add/prospec")
    @ApiOperation("新增一条规格信息")
    public R addProspec(@RequestBody GoodsSpecEntity goodsSpecEntity){
        goodsService.insertProspec(goodsSpecEntity);
        return R.ok();
    }

    @GetMapping("/top10")
    @ApiOperation("查询商品销售榜前5")
    public R top10(){
        return R.ok().put("list",goodsService.getTopSales());
    }

    @GetMapping("/top10/shop")
    @ApiOperation("查询商店销售榜前5")
    public R shopTop5(){
        return R.ok().put("list",goodsService.getTopShop());
    }


}
